//
//  VideoDetailViewController.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/18.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "VideoDetailTableViewCell.h"
#import "VideoCommentTableViewCell.h"
#import "CustomerNetworking.h"
#import "Channel.h"
#import "AutoadaptationFrame.h"
#import "CustomerAVPlayer.h"
#import "CustomerAvplayer.h"
#import <MJRefresh.h>
#import "DiscussModel.h"
#import "VideoCommentTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "UMSocial.h"
#import "SCCatWaitingHUD.h"


@interface VideoDetailViewController ()<UITableViewDataSource, UITableViewDelegate, ShareButtonDelegate>

@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic, retain)Channel *channel;

@property(nonatomic, retain)NSMutableArray *modelArray;

@property(nonatomic, retain)UIImageView *headerImage;

@end

@implementation VideoDetailViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"详情";
    self.modelArray = [NSMutableArray array];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    
    [self createView];
    [self loadData];
}

- (void)loadData
{
    [[SCCatWaitingHUD sharedInstance] animateWithInteractionEnabled:YES];
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.miaopai.com/m/v2_channel.json?fillType=255&likeStat=true&os=ios&scid=%@&unique_id=d148d7c2b9c27f685d9be7639517be5e3661826668&vend=miaopai&version=6.2.1", self.scid] andparameters:nil andBlock:^(id response) {
        [[SCCatWaitingHUD sharedInstance] stop];
        self.channel = [Channel channelWithDic:[response objectForKey:@"result"]];
        
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _channel.pic.base, _channel.pic.m]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        
        CustomerAVPlayer *avplayer = [CustomerAVPlayer shareCustomerAVPlayer];
        avplayer.frame = CGRectMake(0, 0, kWidth, kWidth);
        avplayer.playURL = [NSString stringWithFormat:@"%@%@.%@", _channel.stream.base, _channel.scid, _channel.stream.and];
        [self.headerImage addSubview:avplayer];
        
        [self.tableView reloadData];
    }];
    
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.miaopai.com/m/v2_comment.json?os=ios&page=1&per=20&scid=%@&unique_id=d148d7c2b9c27f685d9be7639517be5e3661826668&version=6.2.1", self.scid] andparameters:nil andBlock:^(id response) {
        NSArray *bigArray = [response objectForKey:@"result"];
        for (NSDictionary *dic in bigArray) {
            DiscussModel *discussModel = [DiscussModel discussModelWithDictionary:dic];
            [_modelArray addObject:discussModel];
        }
        if (_modelArray.count == 0) {
            
            UILabel *nodiscussLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kWidth + 120 + [AutoadaptationFrame heightOfLabel:self.channel.ext.t andTextFont:[UIFont systemFontOfSize:17] andLabelWidth:kWidth - 20], self.view.frame.size.width, 20)];
            nodiscussLabel.text = @"当前暂时没有评论";
            nodiscussLabel.textColor = [UIColor colorWithRed:0.5807 green:0.5807 blue:0.5807 alpha:1.0];
            nodiscussLabel.textAlignment = NSTextAlignmentCenter;
            [_tableView addSubview:nodiscussLabel];
            [nodiscussLabel release];
        }
        [_tableView reloadData];
    }];
}

- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth)];
    self.headerImage.userInteractionEnabled = YES;
    [self.headerImage setImage:[UIImage imageNamed:@"zhanweitu"]];
    self.tableView.tableHeaderView = self.headerImage;
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, -48, 0)];
    [self.tableView registerClass:[VideoDetailTableViewCell class] forCellReuseIdentifier:@"VideoDetailTableViewCell"];
    [self.tableView registerClass:[VideoCommentTableViewCell class] forCellReuseIdentifier:@"VideoCommentTableViewCell"];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.allowsSelection = NO;
    [_tableView release];
    [_tableView.tableFooterView release];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else
    {
        return _modelArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        VideoDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoDetailTableViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.channel = self.channel;
        cell.discussNumber = _modelArray.count;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else
    {
        VideoCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCommentTableViewCell" forIndexPath:indexPath];
        cell.discussModel = (DiscussModel *)[_modelArray objectAtIndex:indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 70 + [AutoadaptationFrame heightOfLabel:self.channel.ext.t andTextFont:[UIFont systemFontOfSize:17] andLabelWidth:kWidth - 20];
    }else
    {
        DiscussModel *discussModel = (DiscussModel *)[_modelArray objectAtIndex:indexPath.row];
        return 45 + [AutoadaptationFrame heightOfLabel:discussModel.content andTextFont:[UIFont systemFontOfSize:17] andLabelWidth:self.view.frame.size.width - 120];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(self.channel != nil)
    {
        if (self.tableView.contentOffset.y > 20) {
            [[CustomerAVPlayer shareCustomerAVPlayer] stop];
        }
        else
        {
            [[CustomerAVPlayer shareCustomerAVPlayer] palyAVPlayer];
        }
    }
   
    
}

- (void)shareButtonDelegateAction
{
    NSString *videoURLStr = [NSString stringWithFormat:@"%@%@.%@", self.channel.stream.base, self.channel.scid, self.channel.stream.and];
    [UMSocialSnsService presentSnsIconSheetView:self
                            appKey:@"56554e9967e58e6ff800226b"
                                      shareText:videoURLStr
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToDouban,UMShareToRenren,UMShareToTencent,nil]
                                       delegate:nil];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[CustomerAVPlayer shareCustomerAVPlayer] stop];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    UIView * view = [self.tabBarController.view.subviews lastObject];
    [UIView animateWithDuration:0.5 animations:^{
        view.frame = CGRectMake(0, kHeight + 100, kWidth, 48);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
